from werkzeug.routing import BaseConverter
from flask import send_from_directory, redirect
from flask_login import current_user, login_required
from main import app
from script.utils.request_utils import check_and_send_file


# 文件下载器路由
class DownloadConverter(BaseConverter):
    regex = "[^/].*?"
    weight = 200
    part_isolating = False


app.url_map.converters['download'] = DownloadConverter


# 权鉴后下载
@app.route('/data/<int:user_id>/<download:filename>', methods=['GET', 'POST'])
@login_required
def user_data_render(user_id, filename):
    if user_id == current_user.id:
        return send_from_directory("/data" + str(user_id), filename)
    else:
        app.aborter(401)


# 首页跳转
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def dist_root():
    return redirect('/index.html')

